package com.wx.tools.wx;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 * 创建签名
 **/
public class SignUtils {

	/**
	 * @param characterEncoding
	 *            编码格式 utf-8
	 * */
	public static String creatSign(String characterEncoding,
			SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k)
					&& !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + WChatInfo.KEY);
		System.out.println(sb);
		String sign = MD5Utils.MD5Encode(sb.toString(), characterEncoding)
				.toUpperCase();
		System.out.println(sign);
		return sign;
	}
	
	/**
	 * @param characterEncoding
	 *            编码格式 utf-8
	 * */
	public static String creatSign2(String characterEncoding,
			SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) 
					&& !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("secret=" + WChatInfo.Secret);
		System.out.println(sb);
		String sign = MD5Utils.MD5Encode(sb.toString(), characterEncoding)
				.toUpperCase();
		System.out.println(sign);
		return sign;
	}
}